import {showToastError} from "../../utils/tool";
import {simplePost} from "../../utils/request";
import {validateMobile} from "../../utils/validate";

Component({
    options:{
        styleIsolation: 'apply-shared'
    },
    properties: {},
    data: {
        form:{
            loginType: 'password',
            code:'',
        },
    },
    methods: {
        changeLoginType(e){
            const {type} = e.currentTarget.dataset
            this.setData({
                [`form.loginType`]: type,
            })
        },

        codeChange(e){
            const {code} = e.detail
            this.setData({
                [`form.code`]:code,
            })
        },

        submit(){
            const {mobile, password, code, loginType} = this.data.form
            if(!mobile || !validateMobile(mobile)) {
                showToastError("手机号格式错误")
                return;
            }
            if(loginType == 'password' && !password) {
                showToastError("密码错误")
                return;
            }
            if(loginType == 'sms' && !code) {
                showToastError("短信验证码错误")
                return;
            }

            simplePost('/user/login', {
                parent_id: global.share_uid,
                mobile:mobile,
                code:code,
                type:mobile,
            }).then(r => {
                this.triggerEvent('loginSuccess', {
                    token:r
                })
            })
        }
    }
});
